package com.home.job.zh1.job5;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class MorseCode {
    private static String MORSE_CODE = "";
    private static Map<Character, String> MORSE_MAP = new HashMap<>();
    public static void initCode() {
        try {
            String textUrl = System.getProperty("user.dir") + "\\src\\com\\home\\job\\zh1\\job5\\encode.txt";
            File file = new File(textUrl);
            if(file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
                String lineTxt;
                while ((lineTxt = br.readLine()) != null) {
                    MORSE_CODE = lineTxt;
                }
                br.close();
            } else {
                System.out.println("文件不存在!");
            }
        } catch (Exception e) {
            System.out.println("文件读取错误!");
        }
    }
    public static void initMap(){
        MORSE_MAP.put('A', ".-");
        MORSE_MAP.put('B', "-...");
        MORSE_MAP.put('C', "-.-.");
        MORSE_MAP.put('D', "-..");
        MORSE_MAP.put('E', ".");
        MORSE_MAP.put('F', "..-.");
        MORSE_MAP.put('G', "--.");
        MORSE_MAP.put('H', "....");
        MORSE_MAP.put('I', "..");
        MORSE_MAP.put('J', ".---");
        MORSE_MAP.put('K', "-.-");
        MORSE_MAP.put('L', ".-..");
        MORSE_MAP.put('M', "--");
        MORSE_MAP.put('N', "-.");
        MORSE_MAP.put('O', "---");
        MORSE_MAP.put('P', ".--.");
        MORSE_MAP.put('Q', "--.-");
        MORSE_MAP.put('R', ".-.");
        MORSE_MAP.put('S', "...");
        MORSE_MAP.put('T', "-");
        MORSE_MAP.put('U', "..-");
        MORSE_MAP.put('V', "...-");
        MORSE_MAP.put('W', ".--");
        MORSE_MAP.put('X', "-..-");
        MORSE_MAP.put('Y', "-.--");
        MORSE_MAP.put('Z', "--..");

        /* 数字电码0-9 */
        MORSE_MAP.put('0', "-----");
        MORSE_MAP.put('1', ".----");
        MORSE_MAP.put('2', "..---");
        MORSE_MAP.put('3', "...--");
        MORSE_MAP.put('4', "....-");
        MORSE_MAP.put('5', ".....");
        MORSE_MAP.put('6', "-....");
        MORSE_MAP.put('7', "--...");
        MORSE_MAP.put('8', "---..");
        MORSE_MAP.put('9', "----.");

    }

    public static String decryption(){
        StringBuilder plainStr = new StringBuilder(" ");
        for (String codes: MORSE_CODE.split("   ")) {
            String[] ciphertext = codes.split(" ");
            for(int i = 0; i< ciphertext.length; i++){
                String tmp = ciphertext[i];
                if(MORSE_MAP.containsValue(tmp)){
                    for (Map.Entry<Character, String> s : MORSE_MAP.entrySet()) {
                        if(tmp.equals(s.getValue())){
                            plainStr.append(s.getKey().toString());
                        }
                    }
                }
            }
            plainStr.append(" ");
        }
        return plainStr.toString();
    }
    public static void main(String[] args) {
        initMap();
        initCode();
        System.out.println(decryption());
    }
}
